<?php
if(!empty($_POST)){
	// print_r($_SESSION);echo $_POST['verify'];exit;
	if(!empty($_SESSION['Verify'])){
		if(strtolower($_SESSION['Verify'])!=strtolower($_POST['verify'])){
			echo "<script>alert('您输入的验证码不正确！');location='index.php?c=user&a=login';</script>";exit;
		};
		unset($_POST['verify']);
	};
	$reg='/^[_a-zA-Z]\w{5,12}$/';//正则 
	$pswreg='/^\w{6,16}$/';//正则 
	$emailreg='/^[a-z0-9]\w*([\_\-]*)*\w+@(\w+){2,9}\.(com|cn|net|com\.cn)$/';//正则 
	preg_match($reg,$_POST['user'],$preg);
	preg_match($pswreg,$_POST['pwd'],$pwdpreg);
	preg_match($pswreg,$_POST['pwds'],$pwdspreg);
	preg_match($emailreg,$_POST['email'],$emailpreg);
	// print_r($preg);echo '<br>';
	// print_r($pwdpreg);echo '<br>';
	// print_r($pwdspreg);echo '<br>';
	// print_r($emailpreg);
		if($_POST['pwd']!=$_POST['pwds'] || empty($preg) || empty($pwdpreg) || empty($pwdspreg) || empty($emailpreg)){
			 echo "<script>alert('您的输入不正确！');location='index.php?c=user&a=login';</script>";exit;
			};
	unset($_POST['pwds']);
	$data=$_POST;
	$user=$data['user'];
	$email=$data['email'];
	date_default_timezone_set('ASIA/Shanghai');
	$time=date('Y-m-d H:i:s',time());			//时间
	$data['addtime']=$time;
	$data['pwd']=md5($data['pwd']);
	$row=insert('user',$data);
	$id=mysql_insert_id();
		if($row>-1){
				$check=md5($id.time());
				$checkdata=array(
					'user_id'=>$id,
					'check_id'=>$check,
					'time'=>time()
					);
				insert('`check`',$checkdata);
			//邮件发送
			require_once(MODEL_PATH.'PHPMailer/class.phpmailer.php');
			//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

			$mail             = new PHPMailer();

			$body             = '<a href="'.ROOT_PATH.'/mvc/index.php?c=user&a=check&check='.$check.'">猛戳这里激活</a>';//获取html内容

			$mail->IsSMTP(); // telling the class to use SMTP 使用smtp协议发送
			//$mail->Host       = "smtp.126.com"; // SMTP server
			$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
			                                           // 1 = errors and messages
			                                           // 2 = messages only
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->Host       = "smtp.126.com"; // sets the SMTP server
			$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
			$mail->Username   = "teaching_code@126.com"; // SMTP account username
			$mail->Password   = "123123123123";        // SMTP account password

			$mail->SetFrom('teaching_code@126.com', 'COOL');//设置接收来源

			$mail->AddReplyTo("teaching_code@126.com","First Last");//回复邮箱


			$mail->Subject    = "欢迎注册！";//标题

			$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

			$mail->MsgHTML($body);//内容使用html格式

			$mail->AddAddress( $email,$user);//有多个邮箱地址，使用多次

			//$mail->AddAttachment("images/phpmailer.gif");      // attachment 附件
			//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment 多个附件执行多次

			if(!$mail->Send()) {
			  echo '<script>alert ("注册失败！请填写正确邮箱！");</script>';exit;
			} else {
			  echo '<script>alert ("注册成功！请到邮箱进行账号激活！");</script>';exit;
			}


				echo '<script>alert ("注册成功");location="index.php?c=index&a=index";</script>';exit;
		};

};
view();
?>